package csc.daonjpa.java.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import csc.daonjpa.java.controller.EntityManagerFactoryUtil;
import csc.daonjpa.java.domain.Account;

import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.TargetAccount;
import csc.daonjpa.java.domain.Transaction;
import csc.daonjpa.java.domain.Transaction;

/**
 * DAO class for Account entity. This class contains all methods that
 * inserts/updates/deletes account info
 * 
 * @author nvu3
 *
 */
@Component
public class TransactionDAO {

	public List<Transaction> getAllTransaction(String loginId) {
		AccountDAO accDAO = new AccountDAO();
		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		List<Transaction> trans = new ArrayList<Transaction>();
		List<Account> acc = new ArrayList<Account>();
		try {
			entr.begin();
			// Get a list of accounts from DB

			TypedQuery<Transaction> query2 = entityManager.createQuery(
					"SELECT c FROM " + Transaction.class.getName() + " c",
					Transaction.class);

			List<Transaction> listAllTrans = query2.getResultList();
			
			TypedQuery<Customer> query = entityManager.createQuery(
					"SELECT c FROM " + Customer.class.getName()
							+ " c Where c.loginId= :loginId", Customer.class);
			query.setParameter("loginId", loginId);
			System.out.println("1.Get Customer - Transaction");

			Customer cus = (Customer) query.getSingleResult();
			System.out.println("LoginID: " + cus.getLoginId());

			acc = (List<Account>) cus.getAccount();
			
			for(Account a:acc)
			{
				for(Transaction tran:listAllTrans)
				{
					if(a.getAccountName()!=null&&tran.getSourceAccount()!=null)
					{
						if(a.getAccountNumber().equals(tran.getSourceAccount())||a.getAccountNumber().equals(tran.getTargetAccount()))
						{
							trans.add(tran);
						}
					}
				}
			}
			entr.commit();
		} catch (Exception e) {
			System.out.println("Error" + e.getMessage());
			entityManager.close();
		}
		// -----------End transaction-----------

		return trans;
	}
	
	public boolean addTransaction(Transaction trans) {
		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil.createEntityManager();
		
		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();
		
		try {
			// -----------Begin transaction-----------
			entr.begin();
			// Insert a row to Account table
			entityManager.persist(trans);

			entr.commit();
		} catch (Exception e) {
			entityManager.close();
		}
		// -----------End transaction-----------
		
		return true;
	}
	
}
